package com.heyidlebooks.mgr;

/*
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import com.heyidlebooks.exception.LuceneException;
import com.heyidlebooks.model.Book;

public class LuceneMgr {

	private Logger LOG = Logger.getLogger(LuceneMgr.class);
	private static final String INDEX_DIR = "C:\\indexes";
	private IndexWriter writer;

	public LuceneMgr() {
		// the boolean true parameter means to create a new index everytime,
		// potentially overwriting any existing files there.
		FSDirectory dir;
		try {
			dir = FSDirectory.open(new File(INDEX_DIR));
		} catch (IOException e) {
			throw new LuceneException("Error when open index folder. "
					+ e.getMessage());
		}
		try {
			writer = new IndexWriter(dir, new StandardAnalyzer(
					Version.LUCENE_CURRENT), true,
					IndexWriter.MaxFieldLength.UNLIMITED);
		} catch (CorruptIndexException e) {
			throw new LuceneException(e);
		} catch (LockObtainFailedException e) {
			throw new LuceneException(e);
		} catch (IOException e) {
			throw new LuceneException(e);
		}
	}

	public void reindex(List<Book> books) throws LuceneException {
		try {
			for (Book book : books) {
				Document doc = new Document();
				doc.add(new Field("key", book.getId().toString(),
						Field.Store.YES, Field.Index.NO));
				doc.add(new Field("id", book.getId().toString(),
						Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new Field("ISBN", book.getIsbn(), Field.Store.YES,
						Field.Index.ANALYZED));
				doc.add(new Field("ASIN", book.getASIN(), Field.Store.NO,
						Field.Index.ANALYZED));
				doc.add(new Field("name", book.getTitle(), Field.Store.YES,
						Field.Index.ANALYZED));
				doc.add(new Field("description", book.getDescription(),
						Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("description", book.getAuthor(),
						Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("description",
						DateTools.dateToString(book.getPublicationDate(),
								DateTools.Resolution.MINUTE), Field.Store.YES,
						Field.Index.NOT_ANALYZED));
				doc.add(new Field("binding", book.getBinding(),
						Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new Field("small_image", book.getSmallImage(),
						Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new Field("medium_image", book.getMediumImage(),
						Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new Field("large_image", book.getLargeImage(),
						Field.Store.YES, Field.Index.NOT_ANALYZED));

				try {
					writer.addDocument(doc);
				} catch (CorruptIndexException e) {
					LOG.warn("Error when index book, ID: " + book.getId()
							+ ", Message: " + e.getMessage());
				} catch (IOException e) {
					LOG.warn("Error when index book, ID: " + book.getId()
							+ ", Message: " + e.getMessage());
				}

			}
		} finally {
			try {
				if (writer != null) {
					writer.optimize();
					writer.close();
				}
			} catch (CorruptIndexException e) {
				throw new LuceneException(e);
			} catch (IOException e) {
				throw new LuceneException(e);
			}
		}
	}
}
*/
